package castledefense.logic.buildings;

import java.util.ArrayList;

import castledefense.logic.Dot;
import castledefense.logic.Map;
import castledefense.logic.Targetable;

public class EnemyBase extends Building implements Targetable {

	private boolean isLife = true;

	public EnemyBase(Map map, int posX, int posY, int type, int elementType) {
		super(map, posX, posY, 4, type);
		initialLife = 1000;
		life = initialLife;
		setElementType(elementType);
	}

	@Override
	public void update(int deltaTime) {
		dotTime -= deltaTime;

		Dot removable = null;
		for (Dot dot : dotList) {
			if (dot.update(deltaTime)) {
				removable = dot;
			}
		}
		if (removable != null) {
			dotList.remove(removable);
		}
	}

	@Override
	public boolean decreaseHealth(double damage, int element) {
		// TODO Auto-generated method stub
		life -= damage;

		if (isLife && life <= 0) {
			map.removeEnemyBase(this);
			isLife = false;			
			return true;
		}
		return false;


	}

	@Override
	public boolean isLife() {
		// TODO Auto-generated method stub
		return isLife;
	}

	@Override
	public boolean isStun() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isSlow() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public int getStunTime() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getSlowTime() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getType() {
		// TODO Auto-generated method stub
		return elementType;
	}

	@Override
	public boolean stunChance(int time, double chance) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void slow(int duration, double upgradeLevelValue) {
		// TODO Auto-generated method stub

	}

	@Override
	public float getRenderingCenterX() {
		// TODO Auto-generated method stub
		return posX + (float) Math.random() * size * Map.cellWidth;
	}

	@Override
	public float getRenderingCenterY() {
		// TODO Auto-generated method stub
		return posY + (float) Math.random() * size * Map.cellWidth;
	}

	private int dotTime = 0;

	private ArrayList<Dot> dotList = new ArrayList<Dot>();

	/** time and howOften as milliSeconds, */
	@Override
	public void applyDot(int time, int howOften, double damagePerTick,
			int damageType) {

		dotTime = time;
		for (Dot tmp : dotList) {
			if (tmp.getTime() > dotTime) {
				dotTime = tmp.getTime();
			}
		}

		dotList.add(new Dot(time, howOften, damagePerTick, damageType, this));
	}

	@Override
	public boolean isDot() {
		return isLife && dotTime > 0;
	}

	@Override
	public int getDotDuration() {
		//
		return isLife ? dotTime : 0;
	}
}
